﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;


namespace CTEA_INDIA
{
    public partial class MyProfileAT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
          
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("Default.aspx");
            }
            else if (!IsPostBack)
            {
                GetData();

                string[] Details = new string[2];
                int UserId = Convert.ToInt32(Session["UserID"]);
                int login = Convert.ToInt32(Session["login"]);
                Details = BAL.Common.GetLoginDetails(UserId, login);
                spnUsername.Visible = true;
                spnLastUsedDate.Visible = true;
                spnUsername.InnerHtml = "Welcome  " + "<span class=\"spnWelcomeName\">" + Details[0] + "</span> ";
                spnLastUsedDate.InnerHtml = " Last used: " + Details[1];
                BAL.Customers.PopulateLoc(ref ddlLoc);


                if (Session["UserTypeId"].ToString().Equals("1"))
                {
                    grdSuggestion.Visible = true;
                    populateSuggestion();
                }
                else
                {
                    grdSuggestion.Visible = false;
                }

                string[] attn = BAL.Customers.Get_Attandance_By_Id(Convert.ToInt32(Session["UserId"]));

                if (attn[0] == null)
                {
                    Attendance.Text = "Attendance"; 
                }
                else {

                    if (DateTime.Parse(attn[2].ToString()).Date < DateTime.Now.Date || DateTime.Parse(attn[2].ToString()).Month < DateTime.Now.Month || DateTime.Parse(attn[2].ToString()).Year < DateTime.Now.Year)
                    {
                        Attendance.Text = "Attended";
                    }
                    else
                    {
                        Attendance.Text = "Attendance";
                    }
                }

            int userID = Convert.ToInt32(Session["UserID"]);
            int Techid = Convert.ToInt32(Session["TechID"]);
                
                
            int Level1 = BAL.Customers.GetLevels(userID);
            int   Level = Level1 - 1;
            int levelBase = BAL.Customers.GetLevelId(userID);
            int levelBase1 = levelBase - 1;
            DataSet Names = BAL.Customers.GetAllTechName();
            PopulateVisitingCard();
            PopulateIdCard();
            int typeID = Convert.ToInt32( Session["UserTypeId"] );

            string[] usertype =  BAL.Customers.getDesignation(typeID);
            txtVCDesignation.Text = usertype[0];
            txtDesignation.Text = usertype[0];
            txtIDDesignation.Text = usertype[0];

           


            if (Names.Tables[0].Rows.Count > 0)
            {


                phdTbleLevels.Controls.Add(new LiteralControl("<table style=\"text-align: left; font-size: 1em; font-weight: bold;\">"));
                

                if (Level1 == 1)
                {
                    phdTbleLevels.Controls.Add(new LiteralControl(" <tr> <td colspan=\"2\" class=\"approvedStatus\"> Approved  </td> </tr>"));
                    
               }else if(Level1 >1)
                {
                    phdTbleLevels.Controls.Add(new LiteralControl(" <tr> <td colspan=\"2\" class=\"pendingStatus\"> PENDING  </td> </tr>"));
                    
                
                
                }


                foreach (DataRow nm in Names.Tables[0].Rows)
                {
                    if (Techid > Convert.ToInt32(nm["TechType_ID"]))
                    {
                       
                            if (Level < Convert.ToInt32(nm["TechType_ID"]))
                            {

                                phdTbleLevels.Controls.Add(new LiteralControl("<tr><td> <img style=\"width: 20px; height: 20px; border: none;\" alt=\"\" src=\"Images/Flag-green-icon.png\" /></td><td>" + nm["TechType"] + "</td></tr>"));
                                
                            
                            }
                            else if (Level > Convert.ToInt32(nm["TechType_ID"]))
                            {

                                phdTbleLevels.Controls.Add(new LiteralControl("<tr><td> <img style=\"width: 20px; height: 20px; border: none;\" alt=\"\" src=\"Images/Upright-icon-GREY.png\" /> </td><td>" + nm["TechType"] + "</td></tr>"));
                               
                            }
                            else if (Level == Convert.ToInt32(nm["TechType_ID"]))
                            {

                                phdTbleLevels.Controls.Add(new LiteralControl("<tr><td> <img style=\"width: 20px; height: 20px; border: none;\" alt=\"\" src=\"Images/Upright-icon.png\" /> </td><td>" + nm["TechType"] + "</td></tr>"));
                             }

                        }

                } 
                    phdTbleLevels.Controls.Add(new LiteralControl("</table>"));
                    
               
            }






            if (Names.Tables[0].Rows.Count > 0)
            {


                placeHolIdCard.Controls.Add(new LiteralControl("<table style=\"text-align: left; font-size: 1em; font-weight: bold;\">"));


                if (levelBase == 1)
                {
                    placeHolIdCard.Controls.Add(new LiteralControl(" <tr> <td colspan=\"2\" class=\"approvedStatus\"> Approved  </td> </tr>"));

                }
                else if (levelBase > 1)
                {
                    placeHolIdCard.Controls.Add(new LiteralControl(" <tr> <td colspan=\"2\" class=\"pendingStatus\"> PENDING  </td> </tr>"));



                }


                foreach (DataRow nm in Names.Tables[0].Rows)
                {
                    if (Techid > Convert.ToInt32(nm["TechType_ID"]))
                    {

                        if (levelBase1 < Convert.ToInt32(nm["TechType_ID"]))
                        {

                            placeHolIdCard.Controls.Add(new LiteralControl("<tr><td> <img style=\"width: 20px; height: 20px; border: none;\" alt=\"\" src=\"Images/Flag-green-icon.png\" /></td><td>" + nm["TechType"] + "</td></tr>"));


                        }
                        else if (levelBase1 > Convert.ToInt32(nm["TechType_ID"]))
                        {

                            placeHolIdCard.Controls.Add(new LiteralControl("<tr><td> <img style=\"width: 20px; height: 20px; border: none;\" alt=\"\" src=\"Images/Upright-icon-GREY.png\" /> </td><td>" + nm["TechType"] + "</td></tr>"));

                        }
                        else if (levelBase1 == Convert.ToInt32(nm["TechType_ID"]))
                        {

                            placeHolIdCard.Controls.Add(new LiteralControl("<tr><td> <img style=\"width: 20px; height: 20px; border: none;\" alt=\"\" src=\"Images/Upright-icon.png\" /> </td><td>" + nm["TechType"] + "</td></tr>"));
                        }

                    }

                }
                placeHolIdCard.Controls.Add(new LiteralControl("</table>"));


            }
        }

        }
            
            
         

       
        protected void IsNotFutureDate(object sender, ServerValidateEventArgs args)
        {
            DateTime dt;

            bool IsDate = DateTime.TryParse(args.Value.ToString(), out dt);

            if (IsDate)
            {
                if (dt.Date <= DateTime.Now.Date)
                    args.IsValid = true;
                else
                    args.IsValid = false;

            }

        }


        public void GetData()
        {

            int userid = Convert.ToInt32(Session["UserID"]);
            string[] details = BAL.Customers.GetProfadmintech(userid);
            lblEmail.Text = details[0];
            txtName.Text = details[1];
            txtAddress.Text = details[2];
            ddlLoc.SelectedValue = details[3];
            txtPers_MobileNo.Text = details[4];
            txtDOB.Text = details[5];

            txtVCName.Text = details[1];
            txtVCContactNumber.Text = details[4];
            txtEmail.Text = details[0];

            txtIDName.Text = details[1];


            txtVCName.ReadOnly = true;
            txtVCDesignation.ReadOnly = true;
            txtVCContactNumber.ReadOnly = true;
            txtEmail.ReadOnly = true;

           cmbMaritial.SelectedValue = details[7];
           txtCorAddr.Text = details[9];
           txtFatherName.Text = details[6];
           txtLanguages.Text =  details[8];

            Disable();
        }

        public void Enabled()
        {

            txtName.ReadOnly = false;
            txtPers_MobileNo.ReadOnly = false;
            txtAddress.ReadOnly = false;
            txtDOB.ReadOnly = false;
            ddlLoc.Enabled = true;
            cmbMaritial.Enabled = true;
            txtCorAddr.Enabled = true;
            //txtDesignation.Enabled = true;
            txtLanguages.Enabled = true ;
            txtFatherName.Enabled = true;
            BtnCancel.Visible = true;
            Button1.Visible = true;
        }


        public void Disable()
        {



            txtName.ReadOnly = true;
            txtPers_MobileNo.ReadOnly = true;
            txtAddress.ReadOnly = true;
            txtDOB.ReadOnly = true;
            ddlLoc.Enabled = false;
            cmbMaritial.Enabled = false;
            txtCorAddr.Enabled = false;
            txtDesignation.Enabled = false;
            txtFatherName.Enabled = false;
            txtLanguages.Enabled = false;

            BtnCancel.Visible = false;
            
            Button1.Visible = false;

        }


        protected void btnEdit_Click1(object sender, EventArgs e)
        {
            Enabled();
            btnEdit.Visible = false;
        }

        protected void BtnCancel_Click1(object sender, EventArgs e)
        {

            Disable();
            btnEdit.Visible = true;
            GetData();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //if (Page.IsValid)
            {
                int userid = Convert.ToInt32(Session["UserID"]);
                string Name = txtName.Text;
                string Address = txtAddress.Text;
                string Location = ddlLoc.SelectedValue;
                DateTime DateOfBirth = Convert.ToDateTime(txtDOB.Text); ;


                string Mobileno = txtPers_MobileNo.Text;
                string fatName = txtFatherName.Text;
                string marStat = cmbMaritial.Text;
                string lang = txtLanguages.Text;
                string corAdd = txtCorAddr.Text;

                BAL.Customers.UpdateProfAdminTech2(Name, Address, Location, DateOfBirth, Mobileno, userid, fatName 
                    , marStat , lang , corAdd);
                GetData();


                Disable();

                idspanupdateMsg.InnerHtml = ("Your Account Updated Successfully...!!");


                btnEdit.Visible = true;





            }
        }

        protected void btnSAve_Click(object sender, EventArgs e)
        {
            string CommuType = ddlSugComType.SelectedValue;
            string Details = txtSugDetails.Text;
            BAL.Customers.InsertDuggestion(CommuType, Details, Session["UserID"].ToString());


            IdSpanSuggestion.Style.Add("display", "block");
            IdSpanSuggestion.InnerHtml = "Your Sugestion Sent to Admin , Thank You";

            txtSugDetails.Text = "";
            ddlSugComType.SelectedIndex = 0;

        }

        protected void btnApplyVC_Click(object sender, EventArgs e)
        {
            //apply for id card
            string Name = txtIDName.Text;
            string EmpCode = lblIDEmpCode.Text;
            string Designtn = txtIDDesignation.Text;
            string Contno = txtVCContactNumber.Text;
            string EmailId = txtEmail.Text;
            string pic = FileUpload1.FileName;
            string UserId = Convert.ToString(Session["UserID"]);
            string TechType = Convert.ToString(Session["TechID"]);

            BAL.Customers.IDCardApply(Name, EmpCode, Designtn, pic, Contno, EmailId, UserId, TechType);


            FileUpload1.SaveAs(Server.MapPath("ProfilePhoto/" + pic));


            //spnMsgVC.Style.Add("display", "block");
           
                

                spnIdErr.Style.Add("display", "block");
                spnIdErr.InnerHtml = "Your Request is sent.";
        }

        public void PopulateVisitingCard()
        {
            int Tectype = Convert.ToInt32(Session["TechID"]);

            DataSet ds = BAL.Customers.PopulateVC(Tectype);
            grdPendingVisiting.DataSource = ds;
            grdPendingVisiting.DataBind();

        }

        public void PopulateIdCard()
        {
            int Tectype = Convert.ToInt32(Session["TechID"]);

            DataSet ds = BAL.Customers.PopulateId(Tectype);
            grdPendingID.DataSource = ds;
            grdPendingID.DataBind();

        }

        public void Clear()
        {
            txtPass.Text = "";
            txtNewPassword.Text = "";
            txtConfirm.Text = "";
        }

        protected void btnChangePass_Click(object sender, EventArgs e)
        {
            int userid = Convert.ToInt32(Session["UserID"]);
            int login = Convert.ToInt32(Session["login"]);
            string EncriptedPasswd = BAL.Encryption.Encript(txtPass.Text, "&%#@?,:*");
            string EncriptedNewPasswd = BAL.Encryption.Encript(txtNewPassword.Text, "&%#@?,:*");

            string strIDUser = BAL.Customers.ChangePassword(userid, login, EncriptedPasswd, EncriptedNewPasswd);

            if (strIDUser == "-1")
            {
                spnErr.Style.Add("display", "block");
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "changeCSSClass", "<script type=\"text/javascript\">$('#idSpanMsg').addClass('spanErrorMsgs');</script>", true);

                spnErr.InnerHtml = "Sorry!!  Password you entered is incorrect";

            }
            else
            {
                spnErr.Style.Add("display", "block");
                spnErr.InnerHtml = "Your Password is Changed Successfully.....";
                Clear();
            }
        }
   
        protected void btnVisitingcCpply_Click(object sender, EventArgs e)
        {
            string Name = txtVCName.Text;
            string EmpCode = lblVCEmpCode.Text;
            string Designtn = txtVCDesignation.Text;
            string Contno = txtVCContactNumber.Text;
            string EmailId = txtEmail.Text;
            string pic = Upic.FileName;
            string UserId = Convert.ToString(Session["UserID"]);
            string TechType = Convert.ToString(Session["TechID"]);

            BAL.Customers.VCardApply(Name, EmpCode, Designtn, pic, Contno, EmailId, UserId, TechType);

            
                Upic.SaveAs(Server.MapPath("ProfilePhoto/" + pic));

            
            spnMsgVC.Style.Add("display", "block");
            spnMsgVC.InnerHtml = "Your Request Sent to the Next Level For Approval";

         txtVCName.Text="";
        lblVCEmpCode.Text="";
       txtVCDesignation.Text="";
       txtVCContactNumber.Text = "";
       txtEmail.Text="";
          
        }

        protected void grdPendingVisiting_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button  lnkApp1 = (Button)e.Row.FindControl("btnApprove");

            }

        }


        protected void grdPendingID_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button lnkApp1 = (Button)e.Row.FindControl("btnApproveId");

            }

        }


        protected void lnkAppr_Click(object sender, EventArgs e)
        {
            LinkButton lnkApp = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkApp.Parent.Parent;

            int Cid = Convert.ToInt32(grdPendingVisiting.DataKeys[row.RowIndex]["HireId"]);
            string[] Details = BAL.Customers.GetHireEquipmentById(Cid);

            string key = Details[24].ToString();

            if (lnkApp.Text.Equals("Disapprove") && (key.Equals("0") || key.Equals("")))
            {
                lnkApp.Text = "Approve";

            }
            else if (lnkApp.Text.Equals("Approve") && key.Equals("0"))
            {
                lnkApp.Text = "Disapprove";
            }
        }
        protected void btnApprove_Click(object sender, EventArgs e)
        {


            Button lnkApp1 = sender as Button;
            GridViewRow row = (GridViewRow)lnkApp1.Parent.Parent;

            int VCid = Convert.ToInt32(grdPendingVisiting.DataKeys[row.RowIndex]["IDVisitingCard"]);
            int TechType = Convert.ToInt32(Session["TechID"]);


            BAL.Customers.UpdateVCApprove(VCid, TechType);

            PopulateVisitingCard();
        
        }


        protected void btnApprove_ClickId(object sender, EventArgs e)
        {


            Button lnkApp1 = sender as Button;
            GridViewRow row = (GridViewRow)lnkApp1.Parent.Parent;

            int VCid = Convert.ToInt32(grdPendingID.DataKeys[row.RowIndex]["IDCard"]);
            int TechType = Convert.ToInt32(Session["TechID"]);


            BAL.Customers.UpdateIdCardApprove(VCid, TechType);

            PopulateIdCard();

        }


        protected void grdSuggestion_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdSuggestion.PageIndex = e.NewPageIndex;
            populateSuggestion();
        }

        protected void grdSuggestion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           

        }

        protected void populateSuggestion()
        {

            DataSet ds = BAL.Customers.getAllSuggestion();
            grdSuggestion.DataSource = ds;
            grdSuggestion.DataBind();

        }

        protected void SugView_Click(object sender, EventArgs e)
        {
            LinkButton lnkVw = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkVw.Parent.Parent;

            int Cid = Convert.ToInt32(grdSuggestion.DataKeys[row.RowIndex]["IDSuggestion"]);

            string[] Details = BAL.Customers.Get_Suggestion_By_Id(Cid);


            lblViewSugName.Text = Details[1];
            lblViewSugUserType.Text = Details[2];
            lblViewSugType.Text = Details[3];
            lblViewSug.Text = Details[4];


            SugView_ModalPopupExtender.Show();

            //document.getElementById("totalDaystd").innerHTML = "<asp:TextBox ID=\"txtDaysText\" runat=\"server\" ReadOnly=\"True\" Text=\""+ days +"\"></asp:TextBox>";

        }


        protected void markAttendance(object sender, EventArgs e)
        {
            string[] attn = BAL.Customers.Get_Attandance_By_Id(Convert.ToInt32(Session["UserId"]));

            if (attn[0] == null)
            {
                Attendance.Text = "Attended";
                BAL.Customers.InsertAttendance(Session["UserId"].ToString());
            }
            else
            {
                if (DateTime.Parse(attn[2].ToString()).Date < DateTime.Now.Date || DateTime.Parse(attn[2].ToString()).Month < DateTime.Now.Month || DateTime.Parse(attn[2].ToString()).Year < DateTime.Now.Year)
                {
                    Attendance.Text = "Attended";
                    BAL.Customers.InsertAttendance(Session["UserId"].ToString());
                }
                    
            }
        }
    }
}
